using UnityEngine;
using System.Collections;

public class ColisionPelota : MonoBehaviour {
	
	//private int speedZ;
	//public Transform bulletPrefab;
    public float shotPower = 15.0f;
	private bool shootIsNeededA = false;
	private bool shootIsNeededB = false;
	private int framesTillRespawn = 0;	
	private float direction;
	private float mag;
	private float initialX = 10f;
	private float initialY = 13f;
	private float initialZ = 20f;
	private bool isDead = false;
    private bool serveA = false;
    private bool serveB = false;
    private bool isFirstHitA = false;
    private bool isFirstHitB = false;
    public int bounceOverTableA = 0;
    public int bounceOverTableB = 0;

    public PointsLogic pointsLogicA;
    public PointsLogic pointsLogicB;

	// Use this for initialization
	void Start () {
 		Vector3 startPos = transform.position;
        Quaternion shotAngle = transform.rotation;
        transform.position = GameObject.Find("RaquetaA").transform.position;
        rigidbody.AddForce(transform.forward * shotPower, ForceMode.Impulse);
        serveB = true;
    }
	
	// Update is called once per frame
	void Update() {
        respawnBall();
        print("A: " + isFirstHitA + "; B: " + isFirstHitB);
		mag = rigidbody.angularVelocity.magnitude;
		direction = rigidbody.velocity.magnitude;
  		if(shootIsNeededA){
			Shoot (180f,(38-(this.transform.position.y))*2);
			shootIsNeededA = false;
		}
		if(shootIsNeededB){
			//print (this.transform.position.y);
			Shoot(-180f,(38-(this.transform.position.y))*2);
			shootIsNeededB = false;		
		}

        if (Input.GetKeyDown("escape"))
        {
            Application.LoadLevel("menu_ppal");
        }

	}
	
	void OnCollisionEnter(Collision collision) {
		  foreach (ContactPoint contact in collision.contacts) {
			if(contact.otherCollider.name.Equals("Red")){
				//print("Estoy en la red");
				//rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                GameObject.Find("SonidoRed").audio.Play();
			}

			else if(contact.otherCollider.name.Equals("MesaDePingPongA")){
				//print("Estoy en la mesa A");             
                GameObject.Find("SonidoRebotePelota").audio.Play();
                bounceOverTableA++;
                bounceOverTableB = 0;
                isFirstHitB = false;

                if (bounceOverTableA == 2)
                {
                    pointsLogicB.sumarPuntos();
                    prepareRespawn();
                    respawnBall();
                }
                else if (isFirstHitA)
                {
                    pointsLogicB.sumarPuntos();
                    isFirstHitA = false;
                    prepareRespawn();
                    respawnBall();
                }
			}
			else if(contact.otherCollider.name.Equals("MesaDePingPongB")){
				//print("Estoy en la mesa B");
                GameObject.Find("SonidoRebotePelota").audio.Play();
                bounceOverTableB++;
                bounceOverTableA = 0;
                isFirstHitA = false;

                if (bounceOverTableB == 2)
                {
                    pointsLogicA.sumarPuntos();
                    prepareRespawn();
                    respawnBall();
                }
                else if (isFirstHitB)
                {
                    pointsLogicA.sumarPuntos();
                    isFirstHitB = false;
                    prepareRespawn();
                    respawnBall();
                }
			}
			else if(contact.otherCollider.name.Equals("RaquetaA")){
				//print("Soy raqueta A");
				shootIsNeededA = true;
                isFirstHitA = true;
                GameObject.Find("SonidoRaquetaA").audio.Play();
			}
			else if(contact.otherCollider.name.Equals("RaquetaB")){
				//print("Soy raqueta B");
				shootIsNeededB = true;
                isFirstHitB = true;
                GameObject.Find("SonidoRaquetaB").audio.Play();
			}
          }
	}
	
	void Shoot (float power,float angle){
		Vector3 startPos = transform.position;
        Vector3 angulo = (new Vector3(0,1,0))*angle;
		if(transform.position.x > 12){
			angulo.x = (Random.value - 0.7f)*2 * 25;
		}else if(transform.position.x < 8){
			angulo.x = (Random.value - 0.3f)*2 * 25;
		}else{
			angulo.x = (Random.value - 0.5f)*2 * 35;
		}
		rigidbody.velocity = new Vector3(0,0,0);
		rigidbody.AddForce((transform.forward * power)+angulo, ForceMode.Impulse);
	}

	void OnTriggerEnter(Collider collision) {
        prepareRespawn();
	}

    void prepareRespawn() {
        isDead = true;
        framesTillRespawn = 50;
    }

    void respawnBall() {
        if (isDead)
        {
            switch (framesTillRespawn)
            {
                case 50:
                    //transform.position = 	new Vector3(initialX,initialY,initialZ);
                    framesTillRespawn--;
                    if (serveB)
                    {
						Vector3 pos = GameObject.Find("RaquetaB").transform.position;
						pos.z -= 0.5f;
                        transform.position = pos;
                        isFirstHitB = true;
                        isFirstHitA = false;
                    }
                    else if (serveA)
                    {
                        transform.position = GameObject.Find("RaquetaA").transform.position;
                        isFirstHitA = true;
                        isFirstHitB = false;
                    }
                    rigidbody.isKinematic = true;
                    break;
                case 0:
                    rigidbody.isKinematic = false;
                    isDead = false;

                    //Codigo agregado
                    if (serveB)
                    {
                        Shoot(-180f,(38-(this.transform.position.y))*2);
                        serveB = false;
                        serveA = true;
                    }
                    else if (serveA)
                    {					
                        Shoot (180f,(38-(this.transform.position.y))*2);
                        serveA = false;
                        serveB = true;
                    }
                    break;
                default:
                    framesTillRespawn--;
                    break;
            }

            bounceOverTableA = 0;
            bounceOverTableB = 0;
        }
    }
	
}
